#include "EvaluatorDataCSReader.h"
#include "reader/NCoreReader.h"
#include "helper/NCorePathHelper.h"

class reader::EvaEnvDataCSReaderPrivate 
{
public:
};

reader::EvaEnvDataCSReader::EvaEnvDataCSReader()
    : d(new EvaEnvDataCSReaderPrivate)
{
}

reader::EvaEnvDataCSReader::~EvaEnvDataCSReader()
{
    delete d;
}

std::map<algobase::EvaluateEnvirDataType, std::map<std::string, darr::DimsArray<float>>> reader::EvaEnvDataCSReader::readData() const
{
    std::map<algobase::EvaluateEnvirDataType, std::map<std::string, darr::DimsArray<float>>> result;

    std::map<std::string, darr::DimsArray<float>> uo_result;
    std::map<std::string, darr::DimsArray<float>> vo_result;

    ncore::helper::PathHelper pathHelper;
    ncore::data::EnvirType EnvirType = ncore::data::EnvirType::ET_CS;

    auto task_config = getTaskConfig();
    ncore::reader::Area area(
        task_config.mMinLongitude, task_config.mMaxLongitude,
        task_config.mMinLatitude, task_config.mMaxLatitude, 0.0, 20000);

    auto read_dates = getReadDates();
    for (const auto& loop_date : read_dates) 
    {
        auto loop_date_str = loop_date.to_string();

        ncore::data::EnvirDate envDate(loop_date.mYear, loop_date.mMonth, loop_date.mDay, loop_date.mHour);
        envDate.setDateType(ncore::data::EnvirDateType::EDT_DayMean);
        std::string loop_file_name = pathHelper.getFilePath(EnvirType, &envDate);
        if (loop_file_name.empty()) {
            continue;
        }

        ncore::reader::NCoreReaderResult<float> loop_read_result;
        ncore::reader::NCoreReader reader(loop_file_name);
        bool read_exec_result = reader.readArea<float>(area, loop_read_result);
        if (!read_exec_result) {
            continue;
        }

        uo_result[loop_date_str] = std::move(loop_read_result["uo"]);
        vo_result[loop_date_str] = std::move(loop_read_result["vo"]);
    }

    result[algobase::EEDT_CS_UO] = std::move(uo_result);
    result[algobase::EEDT_CS_VO] = std::move(vo_result);
    return result;
}
